//获取应用实例
const app = getApp();
Page({
  data: {
    PageCur: 'receipt',
    hasUserInfo: false, //是否获得到用户信息
    screenHeight: 0,
    status0: 0,
    status1: 0
  },
  onLoad: function() {
    wx.showLoading({
      title: '请稍后...',
      mask: true
    });
    //自动获取
    this.getUserInfoAuto();

  },
  onShow() {
    var com = this.selectComponent("#" + this.data.PageCur);
    if(com){
      com.initTabList();
    }
  },
  //用户触发获取用户信息
  getUserInfo: function(e) {
    wx.showLoading({
      title: '请稍后...',
      mask: true
    });
    var detail = e.detail;
    //用户拒绝授权时
    if (detail.errMsg != "getUserInfo:ok") {
      this.getUserInfoAuthAgain();
      return;
    }
    var sessionId = app.globalData.sessionId;
    var that = this;
    var param = {
      sessionKey: sessionId,
      signature: detail.signature,
      rawData: detail.rawData,
      encryptedData: detail.encryptedData,
      iv: detail.iv
    };
    wx.request({
      url: app.url.updateUserInfo,
      method: "POST",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: param,
      success: function(res) {
        wx.hideLoading();
        that.setData({
          hasUserInfo: true
        });
        app.globalData.userInfo = res.data.data;
        app.globalData.loginState = true;
      },
      fail: res => {
        wx.hideLoading();
        wx.showModal({
          content: '更新用户信息出错',
          showCancel: false,
          success: function(res) {}
        });
      }
    });
  },
  getNewCount() {
    var sessionId = app.globalData.sessionId;
    if (!sessionId) {
      return;
    }
    var that = this;
    var param = {
      sessionKey: sessionId
    };
    wx.request({
      url: app.url.getNewCount,
      method: "POST",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: param,
      success: function(res) {
        wx.hideLoading();
        const data = res.data;
        that.setData({
          status0: data.status0,
          status1: data.status1
        });
      },
      fail: res => {
        that.setData({
          status0: 0,
          status1: 0
        });
      }
    });
  },
  //自动获取用户信息(需授权)
  getUserInfoAuto() {
    var that = this;
    //等待登录完成
    if (!app.globalData.loginState) {
      setTimeout(function() {
        console.log("check")
        that.getUserInfoAuto();
      }, 100);
      return;
    }
    this.getNewCount();
    setInterval(function() {
      that.getNewCount();
    }, 2000);
    this.setData({
      screenHeight: app.globalData.screenHeight,
      hasUserInfo: true
    });
    wx.hideLoading();
    if (!app.globalData.userInfo.mobilePhone) {
      wx.navigateTo({
        url: '/pages/register/register'
      });
    }
  },
  //拒绝授权时提示
  getUserInfoAuthAgain() {
    wx.hideLoading();
    var that = this;
    wx.showModal({
      title: '警告',
      showCancel: false,
      confirmText: "知道了",
      content: '请您在询问框中选择"允许"，否则无法登陆！'
    });
  },
  NavChange(e) {
    this.setData({
      PageCur: e.currentTarget.dataset.cur
    })
  },
  onShareAppMessage() {
    return {
      title: '协成派单系统',
      imageUrl: '/images/share.png',
      path: '/pages/index/index'
    }
  }
})